// Toolbar.cpp : Implementation of CToolbar

#include "stdafx.h"
#include "Toolbar.h"



// CToolbar

STDMETHODIMP CToolbar::SetSite(IUnknown* pUnkSite)
{
	if (pUnkSite != NULL)
	{
		HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&m_spWebBrowser);
		if (SUCCEEDED(hr))
		{
			hr = ISinkBrowserEvents::DispEventAdvise(m_spWebBrowser);
			if (SUCCEEDED(hr))
			{
				m_fAdvised = TRUE;
			}
		}
	}
	else
	{
		m_spWebBrowser.Release();
		if (m_fAdvised)
		{
			ISinkBrowserEvents::DispEventUnadvise(m_spWebBrowser);
			m_fAdvised = FALSE;
		}
	}

	return IObjectWithSiteImpl<CToolbar>::SetSite(pUnkSite);
}

void STDMETHODCALLTYPE CToolbar::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{

	HRESULT hr = S_OK;

	CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
	if (spTempWebBrowser && m_spWebBrowser && m_spWebBrowser.IsEqualObject(spTempWebBrowser))
	{
		CComPtr<IDispatch> spDispDoc;
		hr = m_spWebBrowser->get_Document(&spDispDoc);
		if (SUCCEEDED(hr))
		{
			CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
			if (spHTMLDoc != NULL)
			{

				// Hook OnReadyStateChange.  We need to hook even for HTTPS pages, since
				// HTTPS could redirect to HTTP

				if (m_fDocAdvised)
				{
					hr = ISinkDocEvents::DispEventUnadvise(m_spDocument);
					m_fDocAdvised = FALSE;
				}

				hr = ISinkDocEvents::DispEventAdvise(spDispDoc);
				if (SUCCEEDED(hr))
				{
					m_fDocAdvised = TRUE;
					m_spDocument = spDispDoc;
				}
			}

			hr = InjectScriptTags(spDispDoc);
		}
	}
}

void STDMETHODCALLTYPE CToolbar::OnReadyStateChange(IHTMLEventObj *eventObj)
{
	HRESULT hr;

	CComQIPtr<IHTMLDocument2> spHTMLDoc = m_spDocument;
	if (spHTMLDoc == NULL) return;

	CComBSTR readyState;
	hr = spHTMLDoc->get_readyState(&readyState);

	CComBSTR complete("complete");
	CComBSTR loaded("loaded");

	if (readyState == complete || readyState == loaded) 
	{
		InjectScriptTags(m_spDocument);
	}
}

HRESULT STDMETHODCALLTYPE CToolbar::InjectScriptTags(IDispatch *pDoc)
{
	HRESULT hr = E_FAIL;

	CComQIPtr<IHTMLDocument2> spHTMLDoc = pDoc;
	CComQIPtr<IHTMLDocument3> spHTMLDoc3 = pDoc;
	if (spHTMLDoc == NULL || spHTMLDoc3 == NULL) return hr;

	CComBSTR protocol;

	hr = spHTMLDoc->get_protocol(&protocol.m_str);
	if (SUCCEEDED(hr))
	{
		if (!(protocol == CComBSTR("HyperText Transfer Protocol")))
		{
			return S_OK;  // skip all non-HTTP pages
		}
	}

	CComBSTR doctitle;
	spHTMLDoc->get_title(&doctitle.m_str);
	if (FAILED(hr)) return S_OK;

	if (doctitle == CComBSTR(""))
		return S_OK;  // Workaround for WINOOB BUG #1004386, unnecessary on IE8

	CComPtr<IHTMLElementCollection> headTags;
	CComBSTR head("head");

	hr = spHTMLDoc3->getElementsByTagName(head.m_str, &headTags);
	if (SUCCEEDED(hr))
	{
		IDispatch *pDispatch;

		CComVariant idx = 0;
		hr = headTags->item(idx, idx, &pDispatch);
		if (SUCCEEDED(hr))
		{
			CComQIPtr<IHTMLDOMNode> headTag = pDispatch;
			if (headTag != NULL)
			{
				CComBSTR script("SCRIPT");
				CComBSTR src("src");

				CComVariant scriptSrc1("https://visitmix.com/labs/oomph/2.0/client/jquery-1.3.2.min.js");
				InjectTag(pDoc, headTag, script, src, scriptSrc1);

				//CComVariant scriptSrc2("http://localhost/oomph/oomph.js");
				CComVariant scriptSrc2("https://visitmix.com/labs/oomph/2.0/client/oomph.min.js");
				InjectTag(pDoc, headTag, script, src, scriptSrc2);

			}
		}
	}
	return hr;
}

void STDMETHODCALLTYPE CToolbar::InjectTag(IDispatch *pDoc, IHTMLDOMNode *parent, CComBSTR tagName, CComBSTR attrName, CComVariant attrValue)
{
	HRESULT hr;

	CComQIPtr<IHTMLDocument2> spHTMLDoc = pDoc;
	if (pDoc == NULL) return;

	if (TagExists(parent, tagName, attrName, attrValue))
	{
		return;
	}

	CComPtr<IHTMLElement> tag;
	hr = spHTMLDoc->createElement(tagName.m_str, &tag);
	if (SUCCEEDED(hr))
	{
		hr = tag->setAttribute(attrName.m_str, attrValue);
		if (SUCCEEDED(hr))
		{
			CComPtr<IHTMLDOMNode> newNode;
			CComQIPtr<IHTMLDOMNode> tagElement = tag;
			hr = parent->appendChild(tagElement, &newNode);
		}
	}

}

BOOL STDMETHODCALLTYPE CToolbar::TagExists(IHTMLDOMNode *parent, CComBSTR tagName, CComBSTR attrName, CComVariant attrValue)
{
	HRESULT hr = E_FAIL;

	CComQIPtr<IHTMLDOMNode> pTag = parent;
	CComPtr<IHTMLDOMNode> child;

	hr = pTag->get_firstChild(&child);
	if (FAILED(hr)) return FALSE;

	while (child != NULL)
	{
		CComBSTR ctagName;

		hr = child->get_nodeName(&ctagName);
		if (FAILED(hr)) return FALSE;

		if (ctagName == tagName)
		{
			CComPtr<IDispatch> pDispatch;
			hr = child->get_attributes(&pDispatch);
			if (FAILED(hr)) return FALSE;

			CComQIPtr<IHTMLAttributeCollection> attributes = pDispatch;
			if (attributes)
			{
				CComVariant vtAttrName(attrName);
				CComPtr<IDispatch> pDisp2;
				hr = attributes->item(&vtAttrName, &pDisp2);
				if (SUCCEEDED(hr))
				{
					CComQIPtr<IHTMLDOMAttribute> attribute = pDisp2;
					if (attribute)
					{
						CComVariant val;
						hr = attribute->get_nodeValue(&val);
						if (SUCCEEDED(hr))
						{
							if (val == attrValue)
							{
								return TRUE;
							}
						}
					}
				}
			}
		}
		CComPtr<IHTMLDOMNode> next;
		hr = child->get_nextSibling(&next);
		if (SUCCEEDED(hr))
			child = next;
		else return FALSE;
	}

	return FALSE;
}
